// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201109_1.accountmanagement;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201109_1.cm.Operator;
import com.google.api.ads.adwords.axis.v201109_1.mcm.Account;
import com.google.api.ads.adwords.axis.v201109_1.mcm.CreateAccountOperation;
import com.google.api.ads.adwords.axis.v201109_1.mcm.CreateAccountServiceInterface;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.ClientLoginTokens;

/**
 * This example creates a new account under an MCC account.
 *
 * Note: this example must be run using the credentials of an MCC account, and
 * by default the new account will only be accessible via the parent MCC
 * account.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: CreateAccountService.mutate
 *
 * @category adx-exclude
 * @author Adam Rogal
 */
public class CreateAccount {

  public static void main(String[] args) throws Exception {
    // Get a ClientLogin AuthToken.
    String clientLoginToken = new ClientLoginTokens.Builder()
        .forApi(ClientLoginTokens.Api.ADWORDS)
        .fromFile()
        .build()
        .requestToken();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withClientLoginToken(clientLoginToken).build();

    if (session.isEnvironment(AdWordsSession.Environment.SANDBOX)) {
      // To run examples in sandbox, you needed to specify a clientCustomerId.
      // This won't work for this example, so we clear it out here.
      session.setClientCustomerId(null);
    }

    AdWordsServices adWordsServices = new AdWordsServices();

    runExample(adWordsServices, session);
  }

  public static void runExample(
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
    // Get the CampaignService.
    CreateAccountServiceInterface createAccountService =
        adWordsServices.get(session, CreateAccountServiceInterface.class);

    // Create account.
    Account account = new Account();
    account.setCurrencyCode("EUR");
    account.setDateTimeZone("Europe/London");

    // Create operations.
    CreateAccountOperation operation = new CreateAccountOperation();
    operation.setOperand(account);
    operation.setDescriptiveName("Account created with CreateAccountService");
    operation.setOperator(Operator.ADD);

    CreateAccountOperation[] operations = new CreateAccountOperation[] {operation};

    // Add account.
    Account[] result = createAccountService.mutate(operations);

    // Display accounts.
    for (Account accountResult : result) {
      System.out.println("Account with customer ID \"" + accountResult.getCustomerId()
          + "\" was created.");
    }
  }
}
